package com.soriya.nestlive.im.serializer;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.soriya.nestlive.im.constant.MessageType;

import java.lang.reflect.Type;

public class MessageTypeDeserializer implements ObjectDeserializer {
    @Override
    public <T> T deserialze(DefaultJSONParser defaultJSONParser, Type type, Object o) {
        int t = defaultJSONParser.parseObject(Integer.class);
        for (MessageType messageType : MessageType.values()) {
            if (messageType.getType() == t) return (T) messageType;
        }
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return JSONToken.LITERAL_INT;
    }
}
